//
//  productCommentCollectionView.m
//  tabBarText
//
//  Created by tsb1119 on 2017/6/30.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "productCommentCollectionView.h"
#import "productCommentCollectionViewCell.h"
#import "headImageSelectView.h"
#import "TZImagePickerController.h"
//#import "shopTabBarController.h"
@interface productCommentCollectionView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIImagePickerControllerDelegate,UINavigationControllerDelegate,TZImagePickerControllerDelegate>
@property(strong,nonatomic)headImageSelectView * uploadSelectView;
@end

@implementation productCommentCollectionView
-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setupUI];
}
-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if(self = [super initWithFrame:frame collectionViewLayout:layout]){
        UICollectionViewFlowLayout *layout;
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self registerNib:[UINib nibWithNibName:@"productCommentCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"productCommentCollectionViewCell"];
    self.delegate=self;
    self.dataSource=self;
    self.backgroundColor=[UIColor whiteColor];
    self.scrollEnabled=NO;
    self.imgArr = [NSMutableArray arrayWithObject:[UIImage imageNamed:@"add_picture"]];
    [self reloadData];
}

#pragma mark - <UICollectionViewDataSource,UICollectionViewDelegate>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imgArr.count == 4?3:self.imgArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    productCommentCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"productCommentCollectionViewCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    cell.contentView.layer.cornerRadius = 8;
    cell.contentView.layer.masksToBounds = YES;
    [cell setDataWithImage:self.imgArr[indexPath.row] deleteBtnHidden:indexPath.row==self.imgArr.count-1&&self.imgArr.count<5];
    //删除图片
    kWeakSelf;
    cell.deleteBack = ^{
        if (weakSelf.deletePicture) {
            weakSelf.deletePicture(weakSelf.imgArr,indexPath.row);
        }
    };
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //如果是最后一张，而且没有满3张晒图
    if (indexPath.row==self.imgArr.count-1&&indexPath.row!=3) {
        if (self.addPicture) {
            self.addPicture(self.imgArr);
        }
    }
    else
    {
        if (self.showPicture) {
            NSMutableArray * fromFrames = [NSMutableArray array];
            for (int i = 0; i< self.imgArr.count-1; i++) {
                productCommentCollectionViewCell * cell = (productCommentCollectionViewCell*)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
                CGRect fromFrame = [cell.selectImg convertRect:cell.selectImg.bounds toView:[UIApplication sharedApplication].keyWindow];
                [fromFrames addObject:@(fromFrame)];
            }
            self.showPicture(indexPath.row,self.imgArr, fromFrames);
        }
    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(90, 90);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);//分别为上、左、下、右
}
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 0;
}
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
@end
